package com.chrisgrimble.iftu.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class PageDriver {
    public static String getServerResponse(int port, String path) {
        BufferedReader in = null;
        StringBuilder serverResponse = new StringBuilder();
        try {
            URL url = new URL("http://localhost:" + port + path);

            URLConnection urlConnection = url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            InputStreamReader streamReader = new InputStreamReader(inputStream);
            in = new BufferedReader(streamReader);
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                serverResponse.append(inputLine);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return serverResponse.toString();
    }
}
